/*
 * Platzad
 * Copyright 2008 Shayne Riley
 *
 * This file is part of Platzad.
 *
 * Platzad is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Platzad is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Platzad.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact the authors on questions or issues regarding the license,
 * you may send an email to <shayner at gmail dot com> or write to
 * Shayne Riley / P.O. Box 1092 / McCook, NE 69001.
 */

#include "ResLoader.h"
#include <SDL/SDL.h>
#include "SDLDelegate.h" // to load images
#include <iostream>

namespace pzdUtil
{

ResLoader::ResLoader()
{
	pzd::SDLDelegate::getInstance().needSDL(); // To load images
}

ResLoader::~ResLoader()
{
}

pzdDisplay::ImageSDL const * ResLoader::readImage(
		const std::string& filename) const
{
	SDL_Surface *image = SDL_LoadBMP(filename.data());
	if(image == NULL)
	{
		std::cout << "Either not found or usable: file: " << filename.data()
			<< std::endl;
		return NULL;
	}
	return new pzdDisplay::ImageSDL(image);
}

SDL_Surface * ResLoader::copySurface(SDL_Surface *src)
{
	using namespace std;

	if(!copiableSurface(src))
	{
		// Failure
		cerr << __FILE__ << " Unable to copy SDL_Surface, cannot create"
			<< " ImageSDL." << endl;
		return NULL;
	}

	// Create the SDL_Surface. Use the specs from the source image.
	Uint32 flags = src->flags;
	int width = src->w;
	int height = src->h;
	int depth = src->format->BitsPerPixel;
	Uint32 rMask = src->format->Rmask;
	Uint32 gMask = src->format->Gmask;
	Uint32 bMask = src->format->Bmask;
	Uint32 aMask = src->format->Amask;

	SDL_Surface* dest = SDL_CreateRGBSurface(flags, width, height, depth,
			rMask, gMask, bMask, aMask);

	if(dest == NULL)
	{
		cerr << __FILE__ << ": Unable to create surface to copy to. Cannot"
			<< " create ImageSDL." << endl;
		return NULL;
	}

	// Now that the destination is prepared, copy the source to the destination.
	// Neither surface should be locked.
	// If the source copy rectangle or the destination copy rectangle are NULL,
	// then the entire surface is copied, which is what we want.
	if(int error = SDL_BlitSurface(src, NULL, dest, NULL) != 0)
	{
		cerr << __FILE__ << ": Unable to blit. ImageSDL is blank. Error code "
			<< error << "." << endl;
	}

	// The operation was a success, unless the blit failed. Then it is blank.
	return dest;
}

bool ResLoader::copiableSurface(SDL_Surface *src)
{
	using namespace std;

	// If the source is null, then a copy of it cannot exist either.
	if(!src)
	{
		// Failure
		cerr << __FILE__ << ": Source SDL_Surface is null." << endl;
		return false;
	}

	// If the source is locked, then the image cannot be constructed.
	if(src->locked)
	{
		// Failure
		cerr << __FILE__ << ": Source SDL_Surface is locked." << endl;
		return false;
	}

	// SDL_CreateRGBSurface must have SDL_SetVideoMode called first for some
	// reason.
	if(!pzd::SDLDelegate::getInstance().needSDLWithWindow())
	{
		// Failure
		cerr << __FILE__ << ": SDL_SetVideoMode failed." << endl;
		return false;
	}

	// Success!
	// If all the previous checks passed, then it is highly probable that the
	// source surface can be copied from.
	return true;
}

}
